# -*- coding: utf-8 -*-

from urllib.request import urlopen
import re


class GetIPAddress():
    url = 'http://www.ip168.com/'
    pat = re.compile(r'\[((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))\]')

    def __init__(self):
        self.current_ip = None
        self.old_ip = None

    def get_ip(self):
        response = urlopen(self.url)
        html = response.read().decode('utf-8')
        match = self.pat.search(html)
        if match.group():
            self.old_ip = self.current_ip
            self.current_ip = match.group().replace('[', '').replace(']', '')
        else:
            raise Exception('解析IP失败')